package dao.impl;

import dao.FaciDao;
import entity.Faci;
import utils.DBconn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FaciDaoImpl implements FaciDao {
    @Override
    public boolean select(Faci faci) {
        boolean flag=false;
        try {
            DBconn.init();
            ResultSet resultSet = DBconn.selectSql("select * from faci where name='"+faci.getName()+"'");
            while(resultSet.next()){
                if(resultSet.getString("name").equals(faci.getName())){
                    flag = true;
                }
            }
            DBconn.closeConn();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return flag;
    }

    /**
     * 插入
     * @param faci
     * @return
     */
    @Override
    public boolean insert(Faci faci) {
        boolean flag = false;
        DBconn.init();
//        INSERT INTO faci VALUES(null,'xxx','xxx','xxx','xxx','2020-21-21')
        int i = DBconn.addUpdDel("insert into faci values("
                +faci.getId()+",'"+ faci.getName() + "','" + faci.getContractor()
                + "','" + faci.getPhone() + "','" + faci.getLeader() + "','" + faci.getDate()+
                "')");
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();

        return flag;
    }

    /**
     * 删除
     * @param name
     * @return
     */
    @Override
    public boolean delete(String name) {
        boolean flag = false;
        DBconn.init();
        String sql = "delete from faci where id=" + name;
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    /**
     * 更新
     * @param faci
     * @return
     */
    @Override
    public boolean update(Faci faci) {
        boolean flag = false;
        DBconn.init();
        String sql = "update faci set id='" + faci.getId()
                + "',name='" + faci.getName()
                + "',contractor='" + faci.getContractor()
                + "',phone='" + faci.getPhone()
                + "',leader='" + faci.getLeader()+"','"
                +faci.getDate()+"'";
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        return flag;
    }

    @Override
    public List<Faci> getAllFaci(int page, int limit) {
        List<Faci> list = new ArrayList<Faci>();
        try {
            DBconn.init();
            ResultSet resultSet = DBconn.selectSql("select * from faci limit " + (page - 1) * limit + "," + limit + " ");

            while (resultSet.next()) {
                Faci faci = new Faci();
                faci.setId(resultSet.getInt("id"));
                faci.setName(resultSet.getString("name"));
                faci.setContractor(resultSet.getString("contractor"));
                faci.setPhone(resultSet.getString("phone"));
                faci.setLeader(resultSet.getString("leader"));
                faci.setDate(resultSet.getString("date"));
                list.add(faci);
            }
            DBconn.closeConn();
            return list;

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public int getCountByFaci() {
        int count=0;
        try {
            DBconn.init();
            ResultSet rs = DBconn.selectSql("select count(*) from faci");
            if (rs.next()) {
                count = rs.getInt(1);

            }
            DBconn.closeConn();
            return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    @Override
    public boolean deleteById(List list) {
        boolean flag = false;
        DBconn.init();
        String sql = "delete  from faci where id=" + list.get(0);
        int i = DBconn.addUpdDel(sql);
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }

    @Override
    public boolean deleteByIds(List list) {

        boolean flag = false;
        DBconn.init();
        StringBuilder sql = new StringBuilder("delete  from faci where id in( ");
        for (int i = 0; i < list.size(); i++) {
            sql.append("" + list.get(i) + ",");
        }

        sql.replace(sql.lastIndexOf(","), sql.lastIndexOf(",") + 1, ")");

        int i = DBconn.addUpdDel(sql.toString());
        if (i > 0) {
            flag = true;
        }
        DBconn.closeConn();
        return flag;
    }
}

